<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>情侣列表 - 朋友管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            color: #333;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .logo-section {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .logo {
            width: 40px;
            height: 40px;
            filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
        }
        
        .header h1 {
            font-size: 24px;
            font-weight: 500;
        }
        
        .back-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            border: 1px solid rgba(255, 255, 255, 0.3);
            padding: 8px 16px;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            transition: all 0.3s ease;
        }
        
        .back-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .main-content {
            max-width: 1200px;
            margin: 30px auto;
            padding: 0 20px;
        }
        
        .toolbar {
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .add-btn {
            background: #e91e63;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            transition: background 0.3s ease;
        }
        
        .add-btn:hover {
            background: #c2185b;
        }
        
        .couples-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 20px;
        }
        
        .couple-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
            border: 2px solid #f8f9fa;
        }
        
        .couple-card:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
            border-color: #e91e63;
        }
        
        .couple-header {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }
        
        .couple-names {
            font-size: 20px;
            font-weight: 600;
            color: #333;
            margin-bottom: 10px;
        }
        
        .couple-heart {
            color: #e91e63;
            font-size: 24px;
            margin: 0 10px;
        }
        
        .days-counter {
            background: linear-gradient(135deg, #e91e63 0%, #9c27b0 100%);
            color: white;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 14px;
            font-weight: 500;
            display: inline-block;
        }
        
        .couple-info {
            margin-bottom: 20px;
        }
        
        .info-item {
            display: flex;
            margin-bottom: 10px;
            font-size: 14px;
        }
        
        .info-label {
            color: #666;
            min-width: 80px;
            font-weight: 500;
        }
        
        .info-value {
            color: #333;
            flex: 1;
        }
        
        .couple-actions {
            display: flex;
            gap: 10px;
        }
        
        .action-btn {
            flex: 1;
            padding: 10px 15px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 13px;
            transition: all 0.3s ease;
            text-decoration: none;
            text-align: center;
        }
        
        .end-btn {
            background: #ff9800;
            color: white;
        }
        
        .end-btn:hover {
            background: #f57c00;
        }
        
        .delete-btn {
            background: #e74c3c;
            color: white;
        }
        
        .delete-btn:hover {
            background: #c0392b;
        }
        
        .empty-state {
            text-align: center;
            padding: 80px 20px;
            color: #666;
        }
        
        .empty-state h3 {
            font-size: 24px;
            margin-bottom: 15px;
            color: #333;
        }
        
        .empty-state p {
            font-size: 16px;
            margin-bottom: 25px;
        }
        
        .loading {
            text-align: center;
            padding: 60px;
            color: #666;
        }
        
        .auto-update-info {
            background: #e3f2fd;
            color: #1976d2;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
            font-size: 14px;
        }
        
        @media (max-width: 768px) {
            .couples-grid {
                grid-template-columns: 1fr;
            }
            
            .couple-actions {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <div class="logo-section">
                <img src="/static/images/logo.svg" alt="朋友管理系统" class="logo">
                <h1>情侣列表</h1>
            </div>
            <a href="/friend/list" class="back-btn">返回朋友列表</a>
        </div>
    </div>
    
    <div class="main-content">
        <div class="auto-update-info">
            <strong>💕 自动更新提醒：</strong>在一起的天数会在每天零点自动更新
        </div>
        
        <div class="toolbar">
            <h2>当前情侣关系</h2>
            <a href="/couple/create" class="add-btn">创建情侣关系</a>
        </div>
        
        <div id="couplesContainer">
            <div class="loading">加载中...</div>
        </div>
    </div>
    
    <script>
        // 页面加载时获取情侣列表
        document.addEventListener('DOMContentLoaded', function() {
            loadCouples();
            // 设置定时器，每分钟检查一次是否需要更新天数
            setInterval(checkAndUpdateDays, 60000);
        });
        
        // 加载情侣列表
        function loadCouples() {
            fetch('/couple/api/list')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        displayCouples(data.data);
                    } else {
                        showError(data.message);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showError('加载情侣列表失败');
                });
        }
        
        // 显示情侣列表
        function displayCouples(couples) {
            const container = document.getElementById('couplesContainer');
            
            if (!couples || couples.length === 0) {
                container.innerHTML = `
                    <div class="empty-state">
                        <h3>💕 还没有情侣关系</h3>
                        <p>点击"创建情侣关系"按钮开始记录你们的美好时光</p>
                        <a href="/couple/create" class="add-btn">创建情侣关系</a>
                    </div>
                `;
                return;
            }
            
            const couplesHTML = couples.map(couple => `
                <div class="couple-card">
                    <div class="couple-header">
                        <div class="couple-names">
                            ${couple.friend1Name || '未知'}
                            <span class="couple-heart">💕</span>
                            ${couple.friend2Name || '未知'}
                        </div>
                        <div class="days-counter">
                            在一起 ${couple.daysTogether || 0} 天
                        </div>
                    </div>
                    
                    <div class="couple-info">
                        <div class="info-item">
                            <span class="info-label">开始日期:</span>
                            <span class="info-value">${couple.startDate || '未设置'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">状态:</span>
                            <span class="info-value">${couple.status === 'active' ? '💕 热恋中' : '💔 已结束'}</span>
                        </div>
                        <div class="info-item">
                            <span class="info-label">创建时间:</span>
                            <span class="info-value">${formatDateTime(couple.createTime)}</span>
                        </div>
                    </div>
                    
                    <div class="couple-actions">
                        <button class="action-btn end-btn" onclick="endCouple(${couple.id})">结束关系</button>
                        <button class="action-btn delete-btn" onclick="deleteCouple(${couple.id})">删除记录</button>
                    </div>
                </div>
            `).join('');
            
            container.innerHTML = couplesHTML;
        }
        
        // 结束情侣关系
        function endCouple(id) {
            if (!confirm('确定要结束这个情侣关系吗？')) {
                return;
            }
            
            fetch(`/couple/end/${id}`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    loadCouples();
                } else {
                    alert('操作失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('操作失败，请稍后重试');
            });
        }
        
        // 删除情侣关系
        function deleteCouple(id) {
            if (!confirm('确定要删除这个情侣记录吗？此操作不可恢复。')) {
                return;
            }
            
            fetch(`/couple/delete/${id}`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    loadCouples();
                } else {
                    alert('删除失败：' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('删除失败，请稍后重试');
            });
        }
        
        // 检查并更新天数
        function checkAndUpdateDays() {
            const now = new Date();
            const hours = now.getHours();
            const minutes = now.getMinutes();
            
            // 在每天零点附近检查是否需要更新
            if (hours === 0 && minutes < 5) {
                loadCouples();
            }
        }
        
        // 格式化日期时间
        function formatDateTime(dateTimeStr) {
            if (!dateTimeStr) return '未知';
            const date = new Date(dateTimeStr);
            return date.toLocaleString('zh-CN');
        }
        
        // 显示错误信息
        function showError(message) {
            const container = document.getElementById('couplesContainer');
            container.innerHTML = `
                <div class="empty-state">
                    <h3>出错了</h3>
                    <p>${message}</p>
                    <button onclick="loadCouples()" class="add-btn">重试</button>
                </div>
            `;
        }
    </script>
</body>
</html>
